"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 空字符串
Case Name   : jdbc驱动连接数据库，设置参数accept_empty_str，测试空字符串
Create At   : 2023.08.02
Owner       : z@wan005
Description :
    1. 设置参数为accept_empty_str
    2. 远程复制java脚本至指定主机
    3. 编译java工具
    4. 运行java脚本
    5. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 成功
    4. 成功
    5. 成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class JdbcSupportNull(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.dest_path = os.path.join(macro.DB_MY_PATH,
                                      'guc_0051').replace('\\', '/')
        self.java_name = "guc_accept_empty_str_case0051"
        self.default_value = self.common.show_param('behavior_compat_options')

    def test_null(self):
        text = '-----step1:设置参数为accept_empty_str;expect:成功-----'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            "behavior_compat_options "
                                            "='accept_empty_str'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        sql_cmd = "show behavior_compat_options;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('accept_empty_str',
                         sql_res.splitlines()[-2].strip(), '查询失败')

        self.log.info('----step2:远程复制java脚本至指定主机;expect:成功----')
        self.common.scp_file(self.pri_root,
                             f"{self.java_name}.java", self.dest_path)
        cmd = f'ls {self.dest_path}'
        res = self.pri_root.sh(cmd).result()
        self.assertIn(self.java_name, res, 'step2执行失败')

        self.log.info('----step3:编译java工具;expect:成功----')
        self.pri_root.scp_put(macro.JDBC_PATH,
                              f"{self.dest_path}/postgresql.jar")
        cmd = f"javac -encoding utf-8 -cp " \
              f"{os.path.join(self.dest_path, 'postgresql.jar')} " \
              f"{os.path.join(self.dest_path, f'{self.java_name}.java')}"
        self.log.info(cmd)
        result = self.pri_root.sh(cmd).result()
        self.log.info(result)
        self.assertEqual('', result, 'step3执行失败')

        self.log.info('----step4:运行java脚本;expect:成功，'
                      '设置参数为accept_empty_str，空串原样显示----')
        url_host_port = f"{self.pri_user.db_host}:{self.pri_user.db_port}"
        cmd = f" java -cp {os.path.join(self.dest_path, 'postgresql.jar')}" \
              f":{self.dest_path} " \
              f"{self.java_name} {url_host_port} {self.pri_user.db_name} " \
              f"{self.pri_user.db_user} {self.pri_user.db_password}"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_root, cmd)
        self.log.info(res)
        self.assertIn('col1 = ', res, "执行失败")
        self.assertIn('col2 = 10', res, "执行失败")

    def tearDown(self):
        self.log.info('----step5:清理环境;expect:成功----')
        cmd = f'rm -rf {self.dest_path};' \
              f'if [ -d {self.dest_path} ]; ' \
              f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(cmd)
        rm_res = self.common.get_sh_result(self.pri_root, cmd)
        self.log.info(rm_res)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"behavior_compat_options "
                                            f"='{self.default_value}'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        self.assertEqual('not exists', rm_res, "清理编译脚本失败")
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
